package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.SesUserDao;
import com.bestescortbedsystem.dao.SesUserIdentityDao;
import com.bestescortbedsystem.dao.SesUserOrdinaryDao;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.LoginService;
import com.bestescortbedsystem.util.FindDate;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: LoginServiceImpl
 * Package: com.bestescortbedsystem.service.impl
 *
 * @author: guanzepeng
 * @Date: 2021/2/18 10:53 星期四
 * @Description: 进行用户操作业务层
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

	@Resource
	private SesUserDao sesUserDao;

	@Resource
	private SesUserIdentityDao sesUserIdentityDao;

	@Resource
	private SesUserOrdinaryDao sesUserOrdinaryDao;

	/**
	 * 根据手机号查找用户 因为登录使用的是手机号
	 * @param phone 传入手机号
	 * @return 返回一条指定查询的用户数据如果没有查到则返回null
	 */
	@Override
	public SesUser getFindByName(Long phone) {
		SesUserExample sesUserExample = new SesUserExample();
		sesUserExample.createCriteria().andUserphoneEqualTo(phone);
		List<SesUser> list = sesUserDao.selectByExample2(sesUserExample);
		log.info("执行selectByExample方法的查询结果为:" + list);
		if(list.size() == 0){
			return null;
		}
		SesUser sesUser = list.get(0);
		return sesUser;
	}

	/**
	 * 注册一个普通用户
	 * @param sesUser 传入用户信息
	 * @return 返回Boolean
	 */
	@Override
	public Boolean insertUser(SesUser sesUser) {
		//新增一个普通用户的同时先新增一个用户信息数据
		SesUserIdentity sesUserIdentity = new SesUserIdentity();
		sesUserIdentity.setTypeid(9L);
		sesUserIdentity.setUseridentityisadmin("0");
		boolean b = sesUserIdentityDao.insert(sesUserIdentity) == 1;
		if (!b) {
			return false;
		}
		//查询出新增的用户信息
		SesUserIdentityExample sesUserIdentityExample = new SesUserIdentityExample();
		sesUserIdentityExample.setOrderByClause("useridentityId DESC");
		sesUserIdentityExample.createCriteria().andTypeidEqualTo(9L);
		sesUserIdentityExample.setLimit(1);
		sesUserIdentityExample.setOffset(0L);
		List<SesUserIdentity> sesUserIdentities = sesUserIdentityDao.selectByExample(sesUserIdentityExample);
		//将查询到的用户信息ID放入sesUser中
		sesUser.setUserUserIdentityid(sesUserIdentities.get(0).getUseridentityid());
		sesUser.setUserregistrationtime(FindDate.getDate());
		sesUser.setUsertype(1);
		String password = sesUser.getUserpassword();
		String obj = new SimpleHash("MD5",password,"",1024).toString();
		sesUser.setUserpassword(obj);
		sesUser.setUserlastlogintime(FindDate.getDate());
		sesUser.setUserlogincount(0L);
		//新增一个新用户
		b = sesUserDao.insert(sesUser) == 1;
		if(!b) {
			return false;
		}
		//初始化钱包数据
		SesUserExample sesUserExample = new SesUserExample();
		sesUserExample.createCriteria().andUserUserIdentityidEqualTo(sesUserIdentities.get(0).getUseridentityid());
		List<SesUser> sesUserList = sesUserDao.selectByExample(sesUserExample);
		SesUserOrdinary sesUserOrdinary = new SesUserOrdinary();
		sesUserOrdinary.setUserid(sesUserList.get(0).getUserid());
		sesUserOrdinary.setOrdinarymoney(0f);
		sesUserOrdinary.setOrdinarydeposit(0f);
		b = sesUserOrdinaryDao.insertSelective(sesUserOrdinary) == 1;
		return b;
	}
}
